<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<!DOCTYPE html>
<html>
<!-- SKU样式 -->
<link href='${BASE_PATH}/static/css/sku_style.css' media='all'
	rel='stylesheet' type='text/css' />
<head>
<title>管理平台</title>
<jsp:include page="../../../include/header.jsp"></jsp:include>
<style type="text/css">
table td {
	text-align: center;
	white-space: normal;
	overflow: hidden;
	text-overflow: ellipsis;
}

table th {
	text-align: center;
}
</style>
</head>
<body>
	<div id="wrapper" class="row">
		<h1 class="page-header">商品管理</h1>
		<!-- 搜索框 -->
		<div class="col-lg-12 title-top">
			<label class="search-label">商品编号：</label><input type="text"
				id="sGoodId" value="" class="form-control search-input width200"
				maxlength="21" >
			<label class="search-label">商品名称：</label><input type="text"
				id="sGoodName" value="" class="form-control search-input width200"
				>
			<c:if test="${RoleId==1||RoleId==2}">
				<label class="search-label">选择分公司：</label>
				<select id=sStoreId class="selectpicker pull-left p-l-5"
					multiple="multiple" data-width="120">
					<c:forEach items="${list}" var="Store">
						<option value="${Store.storeid }">${Store.title }</option>
					</c:forEach>
				</select>
			</c:if>
			<div class="clearfix"></div>
			<label class="search-label">商品类别</label> 
				<div class="col-sm-2">
					<select name="sCategoryonid" id="sCategoryonid" class=" form-control">
						<option value="">请选择一级分类</option>
						<c:forEach items="${oneCategories}" var="category" varStatus="s">
							<option value="${category.categoryId}">${category.categoryName}</option>
						</c:forEach>
					</select> 
				</div>
				<div class="col-sm-2">
					<select name="sCategorytwid" id="sCategorytwid" class=" form-control">
						<option value="">请选择二级分类</option>
					</select> 
				</div>
				<div class="col-sm-2">
					<select name="sCategorythid" id="sCategorythid" class=" form-control">
						<option value="">请选择三级分类</option>
					</select> 
				</div>
				<div class="col-sm-2">
					<select name="sCategoryfoid" id="sCategoryfoid" class=" form-control">
						<option value="">请选择四级分类</option>
					</select>
				</div>
			<div class="clearfix"></div>
			<div class=" pull-right">
					<button type="button" class="btn btn-success" onclick="toAdd()">
						<span class="fa fa-plus">添加商品</span>
					</button>
				<button type="button" class="btn btn-default" title="刷新"
						onclick="window.location.reload();">
						<i class="fa fa-refresh"></i> 刷新
				</button>
			</div>
			<div class="title-top">

				<div class="search-input">
					<label class="search-label" >商品状态：</label>
					<div class="search-input">
						<select id="sState" class="selectpicker" data-width="150">
							<option value="0">全部</option>
							<option value="1">上架审核中</option>
							<option value="2">已上架</option>
							<option value="3">已下架</option>
							<option value="4">驳回</option>
						</select>
					</div>
					<label class="search-label" >商品属性：</label>
					<div class="search-input">
						<select id="sGb" class="selectpicker" data-width="150">
							<option value="0">全部</option>
							<option value="1">常规</option>
							<option value="2">团购</option>
						</select>
					</div>
					<button class="btn btn-success search-btn" onclick="query();">确定</button>
					<button class="btn btn-success search-btn" onclick="reset();">重置</button>

				</div>
				<div class="clearfix"></div>
				<div class=" pull-right">
					<c:if
						test="${state==2&&qstate==1&&session.userpopedom.busGoodsBaseDoExcel==1 }">
						<button onclick="doExcel();" class="btn btn-success  width120">导出</button>
					</c:if>
				</div>
		</div>
		</div>
		<div class="col-lg-12 margin-tb">
			<div class="text-muted single-line-text pull-left">
				共 <font color="#428bca" id="dataCount">0</font> 条记录
			</div>
		</div>

		<div class="col-lg-12">

			<div class="table-responsive panel panel-default">
				<table id="dataTable"
					class="table table-hover table-noellipsis table-bordered"
					style="overflow: scroll;">
					<thead>
						<tr>
							<c:if test="${RoleId==2&&power==1}">
							<th width="260px">操作</th>
							<th width="80px;">客户页面</th>
							</c:if>
							<th width="100px">商品</th>
							<th width="100px">商品ID</th>
							<th width="300px">商品名称</th>
							<th width="150px">商品分类</th>
							<th width="100px">商品属性</th>
							<th width="200px">所属分公司</th>
							<!-- <th width="100px">积分类型</th>
								<th width="100px">所需积分</th>
								<th width="100px">所需现金</th> -->
							<th width="100px">拓客状态</th>
							<th width="100px">状态</th>
							<th width="180px">上线时间</th>
							
							<!-- <td style="word-wrap:break-word;word-break:break-all;" width="5px">{{item.name}}</td> -->
						</tr>
					</thead>
					<tbody id="dataList"></tbody>
				</table>
			</div>
			<div class="row">
				<div class="col-lg-12">
					<ul id="dataPagination" class="pagination-sm pull-right"></ul>
				</div>
			</div>
		</div>


	</div>
	<jsp:include page="../../../include/javascripts.jsp"></jsp:include>
	<!-- 异步加载下一页数据后，用模板渲染 -->
	<script type="text/html" id="thumbTpl">
<a href="javascript:void(0)" onclick="Kit.photo('{{thumb}}')" class="center-block thumbnail thumbnail-none-margin width50"><img src="{{thumb}}"></a>
</script>
	<script type="text/html" id="dataTpl">
	{{each list as item}}
	<tr id="item{{item.id}}" data-id="{{item.id}}">
		{{if item.pstate==1}}
			{{if item.state==1}}
				<td>			
                    <a href="javascript:toEdit('{{item.id}}');"><span class="btn">编辑</span></a>
					<a href="javascript:return false;" style="opacity: 0.3"><span class="btn">销售信息</span></a>	
					<a href="javascript:return false;"style="opacity: 0.3"><span class="btn">模板</span></a>			
				    <a href="javascript:return false;" style="opacity: 0.3"><span class="btn">下架</span></a> 
					<a href="javascript:return false;" style="opacity: 0.3"><span class="btn">拓客</span></a>
				</td>
			{{else if item.state==2}}
				<td>						
                   	<a href="javascript:toEdit('{{item.id}}');"><span class="btn">编辑</span></a>
					<a href="javascript:return false;"style="opacity: 0.3"><span class="btn">销售信息</span></a>
					<a href="javascript:return false;"style="opacity: 0.3"><span class="btn">模板</span></a>				
                    <a href="javascript:doDown('{{item.id}}');"><span class="btn">下架</span></a>
                    <a href="javascript:doClerk('{{item.id}}');"><span class="btn">售罄</span></a>
					<a href="javascript:return false;" style="opacity: 0.3"><span class="btn">拓客</span></a>
				</td>
			{{else if item.state==3}}
				<td>
					<a href="javascript:toEdit('{{item.id}}');"><span class="btn">编辑</span></a>
					{{if item.groupbuying!=2}}
						<a href="javascript:sale('{{item.id}}');"><span class="btn">销售信息</span></a>
						<a href="javascript:Tem('{{item.id}}');"><span class="btn">模板</span></a>
					{{else if item.groupbuying==2}}	
						<a href="javascript:return false;"style="opacity: 0.3"><span class="btn">销售信息</span></a>
						<a href="javascript:return false;"style="opacity: 0.3"><span class="btn">模板</span></a>	
					{{/if}}
                    <a href="javascript:doUp('{{item.id}}');"><span class="btn">上架</span></a>
                    <a href="javascript:doClerk('{{item.id}}');"><span class="btn">售罄</span></a>
					<a href="javascript:toToker('{{item.id}}');"><span class="btn">拓客</span></a>
                </td>
            {{else if item.state==4}}
				<td>
					<a href="javascript:toEdit('{{item.id}}');"><span class="btn">编辑</span></a>
					{{if item.groupbuying!=2}}
						<a href="javascript:sale('{{item.id}}');"><span class="btn">销售信息</span></a>
						<a href="javascript:Tem('{{item.id}}');"><span class="btn">模板</span></a>
					{{else if item.groupbuying==2}}	
						<a href="javascript:return false;"style="opacity: 0.3"><span class="btn">销售信息</span></a>
						<a href="javascript:return false;"style="opacity: 0.3"><span class="btn">模板</span></a>	
					{{/if}}				
                    <a href="javascript:BoHui('{{item.id}}');"><span class="btn">驳回原因</span></a>
					<a href="javascript:toToker('{{item.id}}');"><span class="btn">拓客</span></a>
				</td>
			{{/if}}
		{{else if item.pstate==2}}
			<td>
				<a href="javascript:toEdit('{{item.id}}');"><span class="btn">编辑</span></a>
					{{if item.groupbuying!=2}}
						<a href="javascript:sale('{{item.id}}');"><span class="btn">销售信息</span></a>
						<a href="javascript:Tem('{{item.id}}');"><span class="btn">模板</span></a>
					{{else if item.groupbuying==2}}	
						<a href="javascript:return false;"style="opacity: 0.3"><span class="btn">销售信息</span></a>
						<a href="javascript:return false;"style="opacity: 0.3"><span class="btn">模板</span></a>	
					{{/if}}		
				<a href="javascript:doUp('{{item.id}}');"><span class="btn">上架</span></a>
				<a href="javascript:toToker('{{item.id}}');"><span class="btn">拓客</span></a>
			</td>
		{{else if item.pstate==3}}
			<td>
				<a href="javascript:toEdit('{{item.id}}');"><span class="btn">编辑</span></a>
					{{if item.groupbuying!=2}}
						<a href="javascript:sale('{{item.id}}');"><span class="btn">销售信息</span></a>
						<a href="javascript:Tem('{{item.id}}');"><span class="btn">模板</span></a>
					{{else if item.groupbuying==2}}	
						<a href="javascript:return false;"style="opacity: 0.3"><span class="btn">销售信息</span></a>
						<a href="javascript:return false;"style="opacity: 0.3"><span class="btn">模板</span></a>	
					{{/if}}	
				<a href="javascript:doUp('{{item.id}}');"><span class="btn">上架</span></a>
				<a href="javascript:toToker('{{item.id}}');"><span class="btn">拓客</span></a>
			</td>
		{{/if}}
		{{if item.groupbuying==1}}
			<td><a href=" http://yphapi.nczywc.cn/h5/#/pages/product/product?id={{item.id}}" target="_blank"><span class="btn">链接</span></a></td>
		{{else if item.groupbuying==2}}
			<td><a href=" http://yphapi.nczywc.cn/h5/#/pages/product/productGroup?ID={{item.id}}&userId=undefined " target="_blank"><span class="btn">链接</span></a></td>
		{{/if}}
		<td><a href="javascript:void(0)" onclick="Kit.photo('${FTP_PATH}{{item.thumb}}')" class="center-block thumbnail thumbnail-none-margin width50"><img src="${FTP_PATH}{{item.thumb}}"></a></td>
		<td>{{item.id}}</td>
		<td style="WORD-WRAP: break-word"  width="10px" >{{item.name}}</td>
		<td>{{item.oneName}}</td>
		<td>{{item.groupbuying | flagTransform:1,'常规',2,'团购'}}</td>
		<td>{{item.title}}</td>
		<td>
			{{if item.isToker==0}}
					关闭
			{{else if item.isToker==1}}
					<span style="color: #428bca;">打开</span>
			{{/if}}

		</td>	
		{{if item.pstate==1}}
			<td>
				{{if item.state==1}}
					上架审核中
				{{else if item.state==2}}
				<span style="color: #428bca;">已上架</span>
				{{else if item.state==3}}
					<span style="color: #a94442;">已下架</span>
                {{else if item.state==4}}
				<span style="color: #a94442;">被驳回</span>
				{{/if}}
			</td>
		{{else if item.pstate==2}}
			<td>售罄</td>
		{{else if item.pstate==3}}
			<td>降权</td>
		{{/if}}
		<td>{{item.createDate  }}
		</td>	
		
	</tr>
	{{/each}}
</script>
	<script type="text/javascript">

var dataPaginator;
$(query);
function query(){
	var sGoodId = $("#sGoodId").val(),sGoodName = $("#sGoodName").val(),sCategoryonid = $("#sCategoryonid").val(),
		sCategorytwid = $("#sCategorytwid").val(),sCategorythid = $("#sCategorythid").val(), 
		sCategoryfoid = $("#sCategoryfoid").val(),storeid = $("#sStoreId").val(), 
		sState = $("#sState").val(),sGb = $("#sGb").val();
	var storeid;
	if(storeid !=null && storeid !="" && storeid !=undefined){
		storeid=storeid.join("','");
	}
	if(dataPaginator){
		dataPaginator.destroy();
	}//
	dataPaginator = Kit.pagination("#dataPagination","${BASE_PATH}/bus/goods/base/page",{sGb:sGb,sGoodName:sGoodName,sGoodId:sGoodId,sCategoryonid:sCategoryonid,sCategorytwid:sCategorytwid,sCategorythid:sCategorythid,sCategoryfoid:sCategoryfoid,sStoreId:storeid,sState:sState,"pageSize":10},function(result){
		//设置显示最新的数据数量
		$("#dataCount").html(result.object.totalRow);
		//根据模板渲染数据并填充
		$("#dataList").empty().append(template("dataTpl",result.object));
	});

}
function reset(){
	$("#sGoodId").val("");
	$("#sGoodName").val("");
	$("#sSellerId").val("");$("#sSellerId").trigger("change");  $("#sStoreId").val("");$("#sStoreId").trigger("change"); 
	$("#sState").val("0");$("#sState").trigger("change");
	$("#sCategoryonid").val("");$("#sCategoryonid").trigger("change");
	$("#sCategorytwid").val("");$("#sCategorytwid").trigger("change");
	$("#sCategorythid").val("");$("#sCategorythid").trigger("change");
	$("#sCategoryfoid").val("");$("#sCategoryfoid").trigger("change");
	$("#sStoreId").selectpicker("refresh"); $("#sSellerId").selectpicker("refresh");
} 
var detailDialog;
function showGoods(id){
	detailDialog = Kit.dialog("商品编辑","${BASE_PATH}/bus/goods/base/goodDetail/"+id,{closable:true,size:'size-wide'}).open();
}


var bohuilog;
function BoHui(id){
	bohuilog = Kit.dialog("驳回原因","${BASE_PATH}/bus/goods/base/toBoHui?id="+id ,{closable:true}).open();
}
var addDialog;
function toAdd(){
	var state=${state};
	var qstate=${qstate};
	var power=${power};
	var RoleId=${RoleId};
	if(RoleId==1||RoleId==2){
	   if(state==2&&power==1){
	    	addDialog = Kit.dialog("添加","${BASE_PATH}/bus/goods/base/toAdd",{closable:true,size:'size-wide'}).open();
	       }
	   if(state!=2){
		Kit.confirm("提示","请您先提交商家入驻信息")
	     }
	   if(power!=1){
		   Kit.confirm("提示","您的商家权限被关闭，请联系管理员")
		     }
	}else{
		if(state==2&&qstate==1){
			addDialog = Kit.dialog("添加","${BASE_PATH}/bus/goods/base/toAdd",{closable:true,size:'size-wide'}).open();
		}
		if(qstate!=1) {
			Kit.confirm("提示","您的分公司权限被关闭，请联系管理员")
		}
		if(state!=2){
			Kit.confirm("提示","请您先提交商家入驻信息")
		}	
	}
  }
var editDialog;
function toEdit(id){
	editDialog = Kit.dialog("修改","${BASE_PATH}/bus/goods/base/toEdit/"+id,{closable:true,size:'size-wide'}).open();
}

var tokerDialog;
function toToker(id){
	tokerDialog = Kit.dialog("拓客","${BASE_PATH}/bus/goods/base/toToker/"+id,{closable:true,size:'size-wide'}).open();
}
//销售信息
var saleDialog;
function sale(id){
	saleDialog = Kit.dialog("销售信息","${BASE_PATH}/bus/goods/base/sale/"+id,{closable:false,size:'size-wide'}).open();
} 
//模板
var TemDialog;
function Tem(id){
	TemDialog = Kit.dialog("模板信息","${BASE_PATH}/bus/goods/base/Tem/"+id,{closable:true,size:'size-wide'}).open();
} 
function onEditSuccess(newObject){
	$("#item"+newObject.id).replaceWith(template("dataTpl",{"list":[newObject]}));
}

function onLogoSet(id,thumb){
	$("#item"+id+" a").replaceWith(template("thumbTpl",{"thumb":thumb}));
}
function doDown(id){
	Kit.confirm("提示","确定要下架商品吗？",function(ref){
		$.post("${BASE_PATH}/bus/goods/base/doDown",{'id':id},function(result){
			if(result.flag==0){
				onEditSuccess(result.object);
			}else{
				Kit.alert("操作失败");return;
			}
			
		});
	});
}
function doClerk(id){
	Kit.confirm("提示","确定要将商品置成售罄吗？",function(ref){
		$.post("${BASE_PATH}/bus/goods/base/doClerk",{'id':id},function(result){
			if(result.flag==0){
				onEditSuccess(result.object);
			}else{
				Kit.alert("操作失败");return;
			}
			
		});
	});
}
function doUp(id){
	Kit.confirm("提示","确定要重新上该架商品吗？",function(ref){
		$.post("${BASE_PATH}/bus/goods/base/doUp",{'id':id},function(result){
			if(result.flag==0){
				onEditSuccess(result.object);
			}else{
				//Kit.alert("操作失败");return;
				Kit.alert(result.message);return;
			}
		});
	});
}
function doDelete(id){
	Kit.confirm("提示","确定要删除商品吗？",function(ref){
		$.post("${BASE_PATH}/goods/base/delete",{'id':id},function(result){
			if(result.flag==0){
				window.location.reload(true) ;
			}else{
				Kit.alert("删除失败");return;
			}
			
		});
	});
}
function doExcel(){
	
	var sGoodId = $("#sGoodId").val(),sGoodName = $("#sGoodName").val(),sCategoryonid = $("#sCategoryonid").val(), sCategorytwid = $("#sCategorytwid").val(),sCategorythid = $("#sCategorythid").val(), sCategoryfoid = $("#sCategoryfoid").val(),storeid = $("#sStoreId").val(),sStoreId = $("#sStoreId").val(), sState = $("#sState").val();
	var store="";
	 if(sStoreId !=null && sStoreId !="" && sStoreId !=undefined){
			store=sStoreId.join("','");
		}
	window.location.href="${BASE_PATH}/bus/goods/base/doExcel?sGoodId="+sGoodId+"&sGoodName="+sGoodName+"&sStoreId="+store+"&sState="+sState+"&sCategoryonid="+sCategoryonid+"&sCategorytwid="+sCategorytwid+"&sCategorythid="+sCategorythid+"&sCategoryfoid="+sCategoryfoid;
}
//点击事件取下级的值
$("#sCategoryonid").change(function(){
	sCategoryonid=$("#sCategoryonid").val();
	  $.ajax({
           type: "POST",
           url: "${BASE_PATH}/bus/goods/base/sCategoryonid",
           data: {"sCategoryonid" : sCategoryonid},
           dataType: "json",
           traditional: true,
           success: function(data){
			$("#sCategorytwid").empty();
			$("#sCategorytwid").append("<option value=''>请选择二级分类</option>");
			for(var i=0; i<data.object.length;i++){
				$("#sCategorytwid").append("<option value='"+data.object[i].categoryId+"'>"+data.object[i].categoryName+"</option>");
               }
           }
       });
    });
$("#sCategorytwid").change(function(){
	sCategorytwid=$("#sCategorytwid").val();
	  $.ajax({
           type: "POST",
           url: "${BASE_PATH}/bus/goods/base/sCategorytwid",
           data: {"sCategorytwid" : sCategorytwid},
           dataType: "json",
           traditional: true,
           success: function(data){
          	   $("#sCategorythid").empty(); 
               $("#sCategorythid").append("<option value=''>请选择三级分类</option>");
               for(var i=0; i<data.object.length;i++){
              	 $("#sCategorythid").append("<option value='"+data.object[i].categoryId+"'>"+data.object[i].categoryName+"</option>");
               }
           }
       });
    });
$("#sCategorythid").change(function(){
	sCategorythid=$("#sCategorythid").val();
	  $.ajax({
           type: "POST",
           url: "${BASE_PATH}/bus/goods/base/sCategorythid",
           data: {"sCategorythid" : sCategorythid},
           dataType: "json",
           traditional: true,
           success: function(data){
          	   $("#sCategoryfoid").empty(); 
               $("#sCategoryfoid").append("<option value=''>请选择四级分类</option>");
               for(var i=0; i<data.object.length;i++){
              	 $("#sCategoryfoid").append("<option value='"+data.object[i].categoryId+"'>"+data.object[i].categoryName+"</option>");
               }
           }
       });
    });
//在表格中显示图片
</script>
</body>
</html>